﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class UserLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (BasicPage.CheckAgent())
            {
                Server.Transfer("~/MobileLogin.aspx");
            }
        }
    }

    protected void LoginIn_Click(object sender, EventArgs e)
    {
        string sqlStr = "SELECT count(1) FROM USER_INFO " +
                             "WHERE UPPER(ULOGIN)=UPPER(:USER_LOGIN) AND UPASS=:USER_PASS " +
                               "AND IFValid='Y'";
        OracleParameter[] _param ={
			    new OracleParameter(":USER_LOGIN",OracleType.VarChar),
                new OracleParameter(":USER_PASS",OracleType.VarChar)
			};
        _param[0].Value = User_Login.Text.Trim();
        _param[1].Value = User_Pass.Text.Trim();
        int count = Int32.Parse(OracleHelper.ExecuteScalar(sqlStr, CommandType.Text, _param).ToString());
        Session["LoginUser"] = count > 0 ? User_Login.Text.Trim().ToLower() : null;
        if (Session["LoginUser"] == null)
        {
            LoginFail();
        }
        else
        {
            LoginSuccess();
        }
        User_Pass.Text = String.Empty;
    }

    private void LoginSuccess()
    {
        if (User_Pass.Text.Trim().Equals(BasicPage.DefaultPass))
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "resetpass"
                , "$(function(){$('#ChangePass').dialog('open');});", true);
        }
        else
        {
            log4net.LogManager.GetLogger(GetType()).Info("{ClientIP:'" + BasicPage.GetClientIP() + "',Msg:'" 
                + Session["LoginUser"].ToString() + " login.'}");
            Response.Redirect("~/Show/Index.aspx");
        }
    }

    private void LoginFail()
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "logfail"
            , @"$(function(){alert('用户名或者密码有误！\n注意:密码区分大小写。');});", true);
        User_Pass.Focus();
    }

    protected void BtnSave_Click(object sender, EventArgs e)
    {
        string sqlStr = "UPDATE user_info SET UPASS =:USER_PASS WHERE ULOGIN =:USER_LOGIN";
        OracleParameter[] _params = {
                new OracleParameter(":USER_PASS",OracleType.VarChar,200),
                new OracleParameter(":USER_LOGIN",OracleType.VarChar,20)
            };
        _params[0].Value = User_Pass_R.Text.Trim();
        _params[1].Value = Session["LoginUser"].ToString();
        OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _params);
        User_Pass_R.Text = "";
        User_Pass_RT.Text = "";
        Page.ClientScript.RegisterStartupScript(GetType(), "resetAlert"
            , "$(function(){alert('密码修改成功，请使用新密码重新登录系统！');});", true);
        User_Pass.Focus();
    }
}
